﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAKS.Models;
using SAKS.ViewModels;
using SAKS.IServices;
using SAKS.ActionFilters;

namespace SAKS.Controllers
{
    public class OrganizationController : ControllerBase
    {
        public IOrganizationServices OrganizationServices { get; set; }
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            var session = GetCurrentSession(requestContext);
            OrganizationServices.Session = session;

        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(DataTableParameter param)
        {
            int? _start = param.iDisplayStart;
            int? _pageSize = param.iDisplayLength;
            int first = _start ?? 0;
            int pageSize = _pageSize ?? 20;
            int? totalRecords = new int?();
            var ret = OrganizationServices.List(first, pageSize, out totalRecords, param.sSearch);
            var json = Json(
                new dataTableJson
                {
                    sEcho = param.sEcho,
                    iTotalRecords = totalRecords,
                    iTotalDisplayRecords = totalRecords,
                    aaData = ret
                },
                JsonRequestBehavior.AllowGet
            );
            return json;
        }
        //
        // GET: /Organization/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Organization/Create
        [NeedsPersistence]
        [HttpPost]
        public ActionResult Create(Organization model)
        {
            try
            {
                OrganizationServices.Save(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }


        
        //
        // GET: /Organization/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            var o = OrganizationServices.Get(id);
            if (o != null)
                return View(o);
            else
                return HttpNotFound();
        }

        //
        // POST: /Organization/Edit/5
        [NeedsPersistence]
        [HttpPost]
        public ActionResult Edit(Organization model)
        {
            try
            {
                OrganizationServices.Update(model);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        //
        // GET: /Organization/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Organization/Delete/5
        [NeedsPersistence]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult IsOrganizationNotExist(string fieldId, string fieldValue)
        {
            object[] result = new object[2];
            result[0] = fieldId;
            if (OrganizationServices.IsNameUserd(fieldValue))
            {
                result[1] = false;
            }
            else
                result[1] = true;
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
